﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;

namespace SignDetection
{

    class ColorMap
    {
        static Geometry point_A;
        static Geometry point_S;//色系平面原点
        static bool initialized = false;
        public static double offsetx;
        public static double offsety;
        static void Initialize()
        {
            if (initialized)
                return;
            point_A.b = 255;
            point_A.g = 0;
            point_A.r = 0;

            point_S.b = 85;
            point_S.g = 85;
            point_S.r = 85;
            offsety = (255 * Math.Sqrt(2.0)) / 2;
            offsetx = offsety * Math.Tan(Math.PI / 6) + 2;
        }
        static void Polarize(Geometry point_S, Geometry point_A, Geometry point_E, ref double angle, ref double seLength)
        {
            double sex = point_E.b - point_S.b;
            double sey = point_E.g - point_S.g;
            double sez = point_E.r - point_S.r;
            double sax = point_A.b - point_S.b;
            double say = point_A.g - point_S.g;
            double saz = point_A.r - point_S.r;
            //double spx = pixel.B - point_S.b;
            //double spy = pixel.G - point_S.g;
            //double spz = pixel.R - point_S.r;

            seLength = Math.Sqrt(sex * sex + sey * sey + sez * sez);
            double saLength = Math.Sqrt(sax * sax + say * say + saz * saz);
            if (seLength != 0)
            {
                double saMse = sex * sax + sey * say + sez * saz;
                if (saMse / (seLength * saLength) <= 1 && saMse / (seLength * saLength) >= -1)
                {
                    angle = Math.Acos(saMse / (seLength * saLength));
                    if (point_E.g < point_E.r)
                        angle=-angle;
                    //angle = angle * 180 / Math.PI;
                    //double distance = Math.Sqrt(spx * spx + spy * spy + spz * spz);
                    //if (angle > 0.866 && point_E.r > 85)
                    //    cls_bitmap.SetPixel(i, j, pixel);
                    //else
                    //    cls_bitmap.SetPixel(i, j, Color.FromArgb(0, 0, 0));
                    //if (point_E.g < 85)
                        //angle = angle + 180;
                      //  seLength = -seLength;
                    //////

                    ///////
                }
            }
        }

        public static Point MapColor(Color pixel)
        {
            Initialize();
            Geometry point_E;//色系射线与色系平面的交点
            point_E.b = 255 * pixel.B / (double)(pixel.R + pixel.G + pixel.B);
            point_E.g = 255 * pixel.G / (double)(pixel.R + pixel.G + pixel.B);
            point_E.r = 255 * pixel.R / (double)(pixel.R + pixel.G + pixel.B);
            double angle = 0;
            double seLength = 0;
            Polarize(point_S, point_A, point_E, ref angle, ref seLength);
            Point point=new Point();
            point.X = (int)(offsetx + (seLength * Math.Cos(angle)));
            point.Y = (int)(offsety + (int)(seLength * Math.Sin(angle)));
            return point;
        }
    }
}

